package com.dongge.action;

import com.dongge.entity.Right;
import com.dongge.entity.Role;
import com.dongge.service.RightService;
import com.dongge.service.RoleService;
import com.opensymphony.xwork2.ActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;

/**
 * Created by Administrator on 2015/11/3.
 */
@Controller
@Scope("prototype")
public class RoleAction extends BaseAction<Role> implements CURDAction {

    private long [] rightIds;

    @Resource
    private RoleService roleService;

    @Resource
    private RightService rightService;

    public long[] getRightIds() {
        return rightIds;
    }

    public void setRightIds(long[] rightIds) {
        this.rightIds = rightIds;
    }

    public String add() {
        roleService.save(model);
        return "toList";
    }

    public String addPrepare() {
        List<Right> rights = rightService.getAll();
        ActionContext.getContext().put("rights",rights);
        return "addPrepare";
    }

    public String delete() {
        roleService.delete(model.getId());
        return "toList";
    }

    public String edit() {
        roleService.update(model);
        roleService.updateRoleRightById(model.getId(),rightIds);
        return "toList";
    }

    public String editPrepare() {
        Role role = roleService.get(model.getId());
        ActionContext.getContext().getValueStack().push(role);
        List<Right> rights = rightService.getAll();
        ActionContext.getContext().put("rights",rights);
        rightIds = roleService.getRoleRightIdsById(model.getId());
        return "editPrepare";
    }

    public String list() {
        List<Role> roles = roleService.getAll();
        ActionContext.getContext().put("roles",roles);
        return "list";
    }

    /**
     * 获取所有权限以及当前角色的权限
     * @return
     */
    public String rightPrepare(){
        assert (model.getId()!=null);
        List<Right> rights = rightService.getAll();
        ActionContext.getContext().put("rights",rights);
        rightIds = roleService.getRoleRightIdsById(model.getId());
        return "rightPrepare";
    }

    /**
     * 编辑权限
     * @return
     */
    public String right(){
        roleService.updateRoleRightById(model.getId(),rightIds);
        return "toList";
    }
}
